package cn.zd.demo1;

import android.app.PictureInPictureParams;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.text.TextUtils;
import android.util.Log;
import android.util.Rational;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.zd.demo1.service.VideoPlayerService;

public class ScreenActivity extends AppCompatActivity implements View.OnClickListener {

    String videoUrl = "https://media.w3.org/2010/05/sintel/trailer.mp4";
    private TextView tv_1;
    private DeskTopReceiver deskTopReceiver;
    private VideoView videoView;
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        Toast.makeText(this, this.getClass().getName(), Toast.LENGTH_LONG).show();
        tv_1 = findViewById(R.id.tv_1);
        findViewById(R.id.btn_1).setOnClickListener(this);
        findViewById(R.id.btn_0).setOnClickListener(this);
        findViewById(R.id.btn_2).setOnClickListener(this);

        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        deskTopReceiver = new DeskTopReceiver();
        registerReceiver(deskTopReceiver,filter);

        videoView = findViewById(R.id.vv_1);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.setLooping(true);
                mediaPlayer.start();
            }
        });
        // 初始化MediaController
        mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);

        // 播放网络视频
        Uri videoUri = Uri.parse(videoUrl);
        videoView.setVideoURI(videoUri);
        videoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(deskTopReceiver);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        switch (newConfig.orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                tv_1.setText("当前为竖屏");
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                tv_1.setText("当前为横屏");
                break;
        }
    }


    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_1) {
            startActivity(new Intent(this, ScreenLandscapeActivity.class));
        } else if(v.getId() ==R.id.btn_0) {
            enterPicModel();
        } else if(v.getId() ==R.id.btn_2) {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("video/*");
            startActivityForResult(intent, 1);
        }
    }

    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, @NonNull Configuration newConfig) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        if(isInPictureInPictureMode) {
            Toast.makeText(this,"进入了画中画模式",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"退出画中画模式",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedVideoUri = data.getData();
                Toast.makeText(this,"选中的视频地址是"+selectedVideoUri.toString(),Toast.LENGTH_SHORT).show();
                videoView.setVideoURI(selectedVideoUri);
                videoView.start();
            }
        }
    }
    /**
     * 定义返回桌面的广播接收器
     */
    private class DeskTopReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent != null && Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
                String reason = intent.getStringExtra("reason");
                Log.i("screen","reason:"+reason);
                if(!TextUtils.isEmpty(reason) && ("homekey".equals(reason) || "recentapps".equals(reason))) {
                    // 8.0之后才有画中画
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                    && !isInPictureInPictureMode()) {
                        enterPicModel();
                    }
                }
            }
        }
    }
    public void enterPicModel(){
        // 创建画中画的参数构建器
        PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
        // 设置宽高比例，第一个代表宽度比例，第二个代表高度比例
        Rational rational = new Rational(10,5);
        builder.setAspectRatio(rational);
        // 进入画中画模式
        enterPictureInPictureMode(builder.build());
    }
}